package net.mysoft.budgetsoft.system.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.pagemodel.GongNeng;
import net.mysoft.budgetsoft.system.service.GongNengService;
import net.mysoft.framework.authority.AccessType;
import net.mysoft.framework.authority.Permission;
import net.mysoft.framework.base.BaseAction;
import net.mysoft.framework.model.Json;
import net.mysoft.framework.model.TreeNode;
import net.mysoft.framework.util.ExceptionUtil;
import net.mysoft.framework.util.TreeUtil;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;

/**
 * 
 * @author dagongPGP v1.0
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a>
 * 
 * @version $Revision: 1.00 $Date: 2013-12-31 10:56:14
 */
@Action(value = "gongNengAction", results = { 
		@Result(name = "gongNeng", location = "/platform/system/gongNeng.jsp"), 
		@Result(name = "gongNengAdd", location = "/platform/system/gongNengAdd.jsp"),
		@Result(name = "gongNengEdit", location = "/platform/system/gongNengEdit.jsp"), 
		@Result(name = "gongNengView", location = "/platform/system/gongNengView.jsp") })
@Controller("gongNengAction")
public class GongNengAction extends BaseAction implements ModelDriven<GongNeng> {

	private static final Logger logger = Logger.getLogger(GongNengAction.class);
	@Resource
	private GongNengService gongNengService;

	private GongNeng gongNeng = new GongNeng();

	public GongNengService getGongNengService() {
		return this.gongNengService;
	}

	public void setGongNengService(GongNengService gongNengService) {
		this.gongNengService = gongNengService;
	}

	public GongNeng getGongNeng() {
		return this.gongNeng;
	}

	public void setGongNeng(GongNeng gongNeng) {
		this.gongNeng = gongNeng;
	}

	public GongNeng getModel() {
		return gongNeng;
	}

	/**
	 * GongNeng列表页面
	 * 
	 * @return
	 */
	@Permission(access=AccessType.LOGIN)
	public String gongNeng() {
		return "gongNeng";
	}

	/**
	 * GongNeng查看页面
	 * 
	 * @return
	 */
	@Permission(access=AccessType.LOGIN)
	public String gongNengView() {
		return "gongNengView";
	}

	/**
	 * GongNeng添加页面
	 * 
	 * @return
	 */
	@Permission(access=AccessType.LOGIN)
	public String gongNengAdd() {
		return "gongNengAdd";
	}

	/**
	 * GongNeng编辑页面
	 * 
	 * @return
	 */
	@Permission(access=AccessType.LOGIN)
	public String gongNengEdit() {
		return "gongNengEdit";
	}

	/**
	 * 菜单+功能 树
	 */
	@Permission(access=AccessType.LOGIN)
	public void tree() {
		logger.info("********系统菜单加载*********");
		List<GongNeng> list = gongNengService.find(gongNeng);
		List<TreeNode> tree = new ArrayList<TreeNode>();
		for (GongNeng gongNeng : list) {
			TreeNode node = new TreeNode();
			node.setId(gongNeng.getBianHao());
			node.setParentId(gongNeng.getShangJiBH()!= null?gongNeng.getShangJiBH():"root");
			node.setText(gongNeng.getMingCheng());
			tree.add(node);
		}
		super.writeJson(TreeUtil.generateTree(tree,new ArrayList(),gongNeng.getTreeNodeID()!= null?gongNeng.getTreeNodeID():"root"));
	}

	/**
	 * 添加
	 */
	public void add() {
		Json j = new Json();
		try {
			gongNengService.add(gongNeng);
			j.setSuccess(true);
			j.setMsg("添加成功!");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("添加失败！");
		}
		super.writeJson(j);
	}

	/**
	 * 修改
	 */
	public void edit() {
		Json j = new Json();
		try {
			gongNengService.edit(gongNeng);
			j.setSuccess(true);
			j.setMsg("编辑成功!");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("编辑失败！");
		}
		super.writeJson(j);
	}

	/**
	 * 删除
	 */
	public void delete() {
		Json j = new Json();
		try {
			gongNengService.delete(gongNeng);
			j.setSuccess(true);
			j.setMsg("删除成功！");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("删除失败！");
		}
		super.writeJson(j);
	}

	/**
	 * 列表
	 */
	public void datagrid() {
		try {
			super.writeJson(gongNengService.datagrid(gongNeng));
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
		}
	}
}
